package guiLayer;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIManager;

import modelLayer.Test;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class Main extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel pnlOwner;
	private JButton btnForest;
	private GridBagLayout gbl_pnlMenu;
	private JPanel pnlForest;
	private JPanel pnlTree;
	private CardLayout cl_pnlInfo;
	private JPanel pnlInfo;
	private JPanel createOwner;
	private JPanel readOwner;
	private Front front;
	private JPanel readForest;
	private ReadTree readTree;
	private CreaterForest createForest;
	private CreaterTree createTree;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
	    } catch (Throwable e) {
	            e.printStackTrace();
	    }
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//new Test();
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Create the frame.
	 */
	public Main() {
		makePanels();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 756, 485);
		setMinimumSize(new Dimension(756, 485));
		
		JPanel pnlMenu = new JPanel();
		pnlMenu.setBackground(new Color(102, 153, 204));
		getContentPane().add(pnlMenu, BorderLayout.WEST);
		gbl_pnlMenu = new GridBagLayout();
		gbl_pnlMenu.columnWidths = new int[]{98, 0};
		gbl_pnlMenu.rowHeights = new int[]{0, 26, 0, 26, 0, -24, 0, 0, 0};
		gbl_pnlMenu.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_pnlMenu.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		pnlMenu.setLayout(gbl_pnlMenu);
		
		JButton btnOwner = new JButton("Owner");
		btnOwner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				toggleOwnerMenu();
			}
		});
		
		JLabel lblMenu = new JLabel("Menu");
		lblMenu.setFont(new Font("SansSerif", Font.BOLD, 13));
		GridBagConstraints gbc_lblMenu = new GridBagConstraints();
		gbc_lblMenu.insets = new Insets(0, 0, 5, 0);
		gbc_lblMenu.gridx = 0;
		gbc_lblMenu.gridy = 0;
		pnlMenu.add(lblMenu, gbc_lblMenu);
		GridBagConstraints gbc_btnOwner = new GridBagConstraints();
		gbc_btnOwner.fill = GridBagConstraints.BOTH;
		gbc_btnOwner.insets = new Insets(0, 0, 5, 0);
		gbc_btnOwner.gridx = 0;
		gbc_btnOwner.gridy = 1;
		pnlMenu.add(btnOwner, gbc_btnOwner);
		
		pnlOwner = new JPanel();
		pnlOwner.setBackground(new Color(102, 153, 204));
		GridBagConstraints gbc_pnlOwner = new GridBagConstraints();
		gbc_pnlOwner.fill = GridBagConstraints.BOTH;
		gbc_pnlOwner.insets = new Insets(0, 0, 5, 0);
		gbc_pnlOwner.gridx = 0;
		gbc_pnlOwner.gridy = 2;
		pnlMenu.add(pnlOwner, gbc_pnlOwner);
		FormLayout fl_pnlOwner = new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("81px"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				RowSpec.decode("20px"),
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("20px"),});
		pnlOwner.setLayout(fl_pnlOwner);
		
		
		JButton btnCreateOwner = new JButton("Create");
		btnCreateOwner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cl_pnlInfo.show(pnlInfo, "1");
			}
		});
		pnlOwner.add(btnCreateOwner, "2, 1, fill, fill");
		
		JButton btnReadOwner = new JButton("Read");
		btnReadOwner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cl_pnlInfo.show(pnlInfo, "2");
			}
		});
		pnlOwner.add(btnReadOwner, "2, 3, fill, fill");
		
		pnlOwner.setVisible(false);
		
		btnForest = new JButton("Forest");
		btnForest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				toggleForestMenu();
			}
		});
		GridBagConstraints gbc_btnForest = new GridBagConstraints();
		gbc_btnForest.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnForest.anchor = GridBagConstraints.NORTH;
		gbc_btnForest.insets = new Insets(0, 0, 5, 0);
		gbc_btnForest.gridx = 0;
		gbc_btnForest.gridy = 3;
		pnlMenu.add(btnForest, gbc_btnForest);
		
		pnlForest = new JPanel();
		pnlForest.setBackground(new Color(102, 153, 204));
		GridBagConstraints gbc_pnlForest = new GridBagConstraints();
		gbc_pnlForest.insets = new Insets(0, 0, 5, 0);
		gbc_pnlForest.fill = GridBagConstraints.BOTH;
		gbc_pnlForest.gridx = 0;
		gbc_pnlForest.gridy = 4;
		pnlMenu.add(pnlForest, gbc_pnlForest);
		pnlForest.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("81px"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				RowSpec.decode("20px"),
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("20px"),}));
		
		JButton btnCreateForest = new JButton("Create");
		btnCreateForest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cl_pnlInfo.show(pnlInfo, "5");
			}
		});
		pnlForest.add(btnCreateForest, "2, 1");
		
		JButton btnReadForest = new JButton("Read");
		btnReadForest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cl_pnlInfo.show(pnlInfo, "6");
			}
		});
		pnlForest.add(btnReadForest, "2, 3, fill, center");
		
		pnlForest.setVisible(false);
		
		JButton btnTree = new JButton("Tree");
		btnTree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				toggleTreeMenu();
			}
		});
		GridBagConstraints gbc_btnTree = new GridBagConstraints();
		gbc_btnTree.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnTree.insets = new Insets(0, 0, 5, 0);
		gbc_btnTree.anchor = GridBagConstraints.NORTH;
		gbc_btnTree.gridx = 0;
		gbc_btnTree.gridy = 5;
		pnlMenu.add(btnTree, gbc_btnTree);
		
		pnlTree = new JPanel();
		pnlTree.setBackground(new Color(102, 153, 204));
		GridBagConstraints gbc_pnlTree = new GridBagConstraints();
		gbc_pnlTree.insets = new Insets(0, 0, 5, 0);
		gbc_pnlTree.fill = GridBagConstraints.BOTH;
		gbc_pnlTree.gridx = 0;
		gbc_pnlTree.gridy = 6;
		pnlMenu.add(pnlTree, gbc_pnlTree);
		pnlTree.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("81px"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				RowSpec.decode("20px"),
				FormFactory.LINE_GAP_ROWSPEC,
				RowSpec.decode("20px"),}));
		
		JButton btnCreateTree = new JButton("Create");
		btnCreateTree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cl_pnlInfo.show(pnlInfo, "9");
			}
		});
		pnlTree.add(btnCreateTree, "2, 1, fill, fill");
		
		JButton btnReadTree = new JButton("Read");
		btnReadTree.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cl_pnlInfo.show(pnlInfo, "10");
			}
		});
		pnlTree.add(btnReadTree, "2, 3, fill, fill");
		
		JPanel pnlMain = new JPanel();
		getContentPane().add(pnlMain, BorderLayout.CENTER);
		pnlMain.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,}));
		
		pnlInfo = new JPanel();
		pnlMain.add(pnlInfo, "2, 2, fill, fill");
		pnlInfo.setLayout(new CardLayout(0, 0));
		cl_pnlInfo = (CardLayout) pnlInfo.getLayout();
		
		addPanelsToCard();
		
		pnlTree.setVisible(false);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBackground(UIManager.getColor("Button.background"));
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmLoad = new JMenuItem("Load");
		mnFile.add(mntmLoad);
		
		JMenuItem mntmSave = new JMenuItem("Save");
		mnFile.add(mntmSave);
		
		JMenuItem mntmTest = new JMenuItem("Insert Test Data");
		mntmTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				test();
			}
		});
		mnFile.add(mntmTest);
		
		JSeparator separator = new JSeparator();
		mnFile.add(separator);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		mnFile.add(mntmExit);
	}
	
	protected void test() {
		Thread t = new Thread(){
				public void run(){
				new Test();
				String prev = currentCard();
				cl_pnlInfo.show(pnlInfo, "0");
				cl_pnlInfo.show(pnlInfo, prev);
			}
		};
		
		t.start();
	}

	private String currentCard() {
		String ret = "";
		Component[] comps = pnlInfo.getComponents();
		
		boolean found = false;
		int i = 0;
		while(!found && comps.length > i){
			if(comps[i].isVisible()){
				ret = comps[i].getName();
			}
			i++;
		}
		
		return ret;
	}

	protected void makePanels(){
		this.createOwner = new CreaterOwner(null, null);
		this.createForest = new CreaterForest(null, null);
		this.createTree = new CreaterTree(null, null);
		this.readOwner = new ReadOwner();
		this.readForest = new ReadForest();
		this.front = new Front();
		this.readTree = new ReadTree();
	}
	
	protected void addPanelsToCard(){
		pnlInfo.add(front, "0");
		front.setName("0");
		
		pnlInfo.add(createOwner, "1");
		createOwner.setName("1");
		
		pnlInfo.add(readOwner, "2");
		readOwner.setName("2");
		
		pnlInfo.add(createForest, "5");
		createForest.setName("5");
		
		pnlInfo.add(readForest, "6");
		readForest.setName("6");
		
		pnlInfo.add(createTree, "9");
		createTree.setName("9");
		
		pnlInfo.add(readTree, "10");
		readTree.setName("10");
	}

	protected void toggleForestMenu() {
		if(pnlForest.isVisible()){
			gbl_pnlMenu.rowHeights[4] = 0;
			pnlForest.setVisible(false);
		}else{
			gbl_pnlMenu.rowHeights[4] = 51;
			pnlForest.setVisible(true);
		}
	}

	protected void toggleTreeMenu() {
		if(pnlTree.isVisible()){
			gbl_pnlMenu.rowHeights[6] = 0;
			pnlTree.setVisible(false);
		}else{
			gbl_pnlMenu.rowHeights[6] = 51;
			pnlTree.setVisible(true);
		}
	}

	protected void toggleOwnerMenu() {
		if(pnlOwner.isVisible()){
			gbl_pnlMenu.rowHeights[2] = 0;
			pnlOwner.setVisible(false);
		}else{
			gbl_pnlMenu.rowHeights[2] = 51;
			pnlOwner.setVisible(true);
		}
	}
	
}
